{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1e52c564-d40d-4592-9ab4-886d1c079456",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2e89bc99-236a-43de-a2ef-ff4ec4f8104e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=np.random.uniform(0,4,(7,5)) # 生成0，4之间均匀分布的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "18ce204a-c453-409c-835f-6c2df9e48748",
   "metadata": {},
   "outputs": [],
   "source": [
    "def standard_normalization(data):\n",
    "    mean_val = np.mean(data)  # 获取当前数据集所有数据的均值  均值归一化\n",
    "    std_val = np.std(data)  # 获取当前数据集所有数据的标准差  方差归一化\n",
    "    normalized_data = (data - mean_val) / std_val  # 归一化公式\n",
    "    return normalized_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fd202bc0-7d1f-4528-a6ff-2e01ccbb23ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "n_x=standard_normalization(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "44c3e98f-a57d-4dd0-aefe-ad64dad2c20b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[8.15931942e-01 1.34172249e-01 3.13428573e+00 3.66864296e+00\n",
      "  3.42753390e+00]\n",
      " [1.62285899e+00 1.86492296e+00 1.37515328e+00 3.89519705e+00\n",
      "  2.83664060e-03]\n",
      " [7.67812336e-01 9.87550622e-01 2.33100549e-01 3.25808590e-01\n",
      "  2.73969554e+00]\n",
      " [3.42632657e+00 3.97896712e+00 8.84153578e-01 3.31899927e-01\n",
      "  7.62523857e-02]\n",
      " [2.19410680e+00 3.56881527e+00 1.89704963e+00 2.39498954e+00\n",
      "  2.66135770e+00]\n",
      " [3.43408804e+00 2.45382388e-02 3.11095772e+00 5.87176145e-01\n",
      "  4.50147200e-01]\n",
      " [5.46985956e-01 1.40614025e+00 2.72941282e+00 1.17228838e+00\n",
      "  1.85144035e+00]]\n",
      "\n",
      "\n",
      "[[-0.73446047 -1.26289121  1.06249145  1.47667079  1.28978759]\n",
      " [-0.10901274  0.07861061 -0.301009    1.65227247 -1.36468921]\n",
      " [-0.7717579  -0.60143914 -1.18621206 -1.11435422  0.75664529]\n",
      " [ 1.28885179  1.7172025  -0.681582   -1.10963283 -1.30778479]\n",
      " [ 0.33376042  1.39929453  0.10351194  0.48946428  0.69592577]\n",
      " [ 1.29486769 -1.34786833  1.04440995 -0.91176869 -1.01797958]\n",
      " [-0.94292003 -0.27699105  0.74867517 -0.45824923  0.06816027]]\n"
     ]
    }
   ],
   "source": [
    "print(x)\n",
    "print('\\n')\n",
    "print(n_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cb3accc7-71b4-49a7-9fa5-fc4156e1c9f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "scalar=StandardScaler()  #sklearn实现\n",
    "ns_x=scalar.fit_transform(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "641c14d9-32eb-4aa8-ab82-31f02a5fd611",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[8.15931942e-01 1.34172249e-01 3.13428573e+00 3.66864296e+00\n",
      "  3.42753390e+00]\n",
      " [1.62285899e+00 1.86492296e+00 1.37515328e+00 3.89519705e+00\n",
      "  2.83664060e-03]\n",
      " [7.67812336e-01 9.87550622e-01 2.33100549e-01 3.25808590e-01\n",
      "  2.73969554e+00]\n",
      " [3.42632657e+00 3.97896712e+00 8.84153578e-01 3.31899927e-01\n",
      "  7.62523857e-02]\n",
      " [2.19410680e+00 3.56881527e+00 1.89704963e+00 2.39498954e+00\n",
      "  2.66135770e+00]\n",
      " [3.43408804e+00 2.45382388e-02 3.11095772e+00 5.87176145e-01\n",
      "  4.50147200e-01]\n",
      " [5.46985956e-01 1.40614025e+00 2.72941282e+00 1.17228838e+00\n",
      "  1.85144035e+00]]\n",
      "\n",
      "\n",
      "[[-0.73446047 -1.26289121  1.06249145  1.47667079  1.28978759]\n",
      " [-0.10901274  0.07861061 -0.301009    1.65227247 -1.36468921]\n",
      " [-0.7717579  -0.60143914 -1.18621206 -1.11435422  0.75664529]\n",
      " [ 1.28885179  1.7172025  -0.681582   -1.10963283 -1.30778479]\n",
      " [ 0.33376042  1.39929453  0.10351194  0.48946428  0.69592577]\n",
      " [ 1.29486769 -1.34786833  1.04440995 -0.91176869 -1.01797958]\n",
      " [-0.94292003 -0.27699105  0.74867517 -0.45824923  0.06816027]]\n"
     ]
    }
   ],
   "source": [
    "print(x)\n",
    "print('\\n')\n",
    "print(n_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d35229f7-61e3-45a2-a6ca-3bb9f7a5a178",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 标准归一化数据有正有负，可以让wi正向，wj负向迭代\n",
    "# 数据集x分出训练集x1和测试集x2，测试x2时要用x1的均值和方差\n",
    "# 即用来自训练集的持久化scaler去fit测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b3d32b9-3296-466f-ba57-6fd3e20931ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
